package com.example.prison.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.LoginInterceptor;
import com.example.prison.entity.CarAlarmRecord;
import com.example.prison.entity.UseCarManagement;
import com.example.prison.mapper.CarAlarmRecordMapper;
import com.example.prison.service.UseCarManagementService;
import com.example.prison.mapper.UseCarManagementMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author jiang
* @description 针对表【use_car_management】的数据库操作Service实现
* @createDate 2024-06-03 14:14:12
*/
@Service
public class UseCarManagementServiceImpl extends ServiceImpl<UseCarManagementMapper, UseCarManagement>
    implements UseCarManagementService{

    @Autowired
    private UseCarManagementMapper useCarManagementMapper;

    @Autowired
    private CarAlarmRecordMapper carAlarmRecordMapper;

    /** 获取使用车辆列表 */
    @Override
    public PageInfo<UseCarManagement> get(int pageNumber, int pageSize, String carNumber, String usePeople, String status, String useRange, String useStatus, String beginTime, String endTime) {
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(useCarManagementMapper.get(carNumber, LoginInterceptor.loginUser.getUserName(), status, useRange, useStatus, beginTime, endTime));
//        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(useCarManagementMapper.get(carNumber, usePeople, status, useRange, useStatus, beginTime, endTime));
    }

    @Override
    public int add(UseCarManagement useCar) {
        useCar.setStatus("未审批");
        useCarManagementMapper.add(useCar);
        return 1;
    }

    @Override
    public void delete(Integer[] ids) {
        useCarManagementMapper.deleteUseCar(ids);
    }

    @Override
    public int update(UseCarManagement useCar, Integer id) {
        if(ObjectUtils.isEmpty(useCar.getCarNumber())) {
            useCarManagementMapper.updateUseCar(useCar, id);
            return 1;
        }
        UseCarManagement useCarDTO = useCarManagementMapper.getById(id);
        useCarManagementMapper.updateUseCar(useCar, id);
        int res = 0;
        List<UseCarManagement> useCarList = useCarManagementMapper.getOneByNumber(useCar.getCarNumber());
        for(UseCarManagement useOneCar : useCarList) {
            if (ObjectUtils.isNotEmpty(useOneCar)) {
                int beginTimeResult = useCar.getUseTime().compareTo(useOneCar.getUseTime());
                int endTimeResult = useCar.getEndTime().compareTo(useOneCar.getEndTime());
                int beginToEnd = useCar.getUseTime().compareTo(useOneCar.getEndTime());
                int endToBegin = useCar.getEndTime().compareTo(useOneCar.getUseTime());
                if ((endToBegin >= 0 && endTimeResult <= 0) || (beginTimeResult >= 0 && beginToEnd <= 0) || (beginTimeResult <= 0 && endTimeResult >= 0)) {
                    res++;
                }
            }
        }
        if(res > 1){
            return 0;
        }
        if(Objects.equals(useCar.getStatus(), "同意")) {
            useCar.setUseStatus("未出发");
        }
        useCarManagementMapper.updateUseCar(useCar, id);
        return 1;
    }

    @Override
    public UseCarManagement getById(Integer id) {
        return useCarManagementMapper.getById(id);
    }


    /** 获取到期未归用车 */
    @Override
    public PageInfo<UseCarManagement> getNoReturn(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
        List<UseCarManagement> list = useCarManagementMapper.getNoReturn(date);
        for(UseCarManagement car : list) {
            if(ObjectUtils.isEmpty(carAlarmRecordMapper.getOneNoReturnByCarNumber(car.getCarNumber()))) {
                //到期未归告警
                CarAlarmRecord record = new CarAlarmRecord();
                record.setCarNumber(car.getCarNumber());
                record.setTime(date);
                record.setUsePeople(car.getUsePeople());
                record.setType("到期未归");
                record.setSolve("未处理");
                carAlarmRecordMapper.add(record);
            }
        }
        return new PageInfo<>(useCarManagementMapper.getNoReturn(date));
    }

    /** 获取使用中长途用车 */
    @Override
    public PageInfo<UseCarManagement> getUsingLongCar(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(useCarManagementMapper.getUsingLongCar());
    }

    @Override
    public UseCarManagement getOneByNumberAndUseStatus(String carNumber, String status) {
        return useCarManagementMapper.getOneByNumberAndUseStatus(carNumber, status);
    }


}




